/*!  \file  MemoryPool.h
 *	  \brief  
 * 
 *   \details   details
 *   \author    Yordan Madzhunkov German Research School for Simulation Sciences GmbH
 *   \version   1.0
 *   \date      07/17/2013 08:32:27 PM
 *   \copyright GNU Public License.
 */
#pragma once

#ifdef MEMORYPOOLLOGGER
class	MemoryPoolLogger;
#endif
class IDAllocator;
struct ThreadIDAllocator {
	IDAllocator * alloc;
	IDAllocator * deloc;
	void swap() { IDAllocator * x = alloc; alloc = deloc; deloc = x; };
};
class MemoryPool {
public:
	MemoryPool(const int maxSize_, const int maxThreads_);
	virtual ~MemoryPool();
	bool allocate(int & id, const int tID); // thread safe
	bool delocate(const int id, const int tID); // thread safe
	void reset();
#ifdef MEMORYPOOLLOGGER
	MemoryPoolLogger * logger ;
#endif
	int getAllocatorID(const IDAllocator * all) const;
private:
	NoLimitQueue<IDAllocator*> * allocQueue; // allocators that doesn't belong to any1
	NoLimitQueue<IDAllocator*> * delocQueue; // same story here
	ThreadIDAllocator   * threadIDAllocator; // those belong to given thread
	IDAllocator         * allAllocators;
	int 					  * storage;
	int nBlocks, blockSize, maxSize, maxThreads;
};
